@using System.Security.Principal
@using System.Security.Claims
@using Microsoft.AspNetCore.Identity
@using MusicStore.Models

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@{
    //Func<string, HtmlString> js = input => Html.Raw(HttpUtility.JavaScriptStringEncode(input, false));
}

@if (SignInManager.IsSignedIn(User))
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + UserManager.GetUserName(User) + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    
    @Html.Json(new {
        isAuthenticated = true,
        userName = UserManager.GetUserName(User),
        userId = UserManager.GetUserId(User),
        roles = ((ClaimsPrincipal)User).Claims
            .Where(c => c.Type == ClaimTypes.Role)
            .Select(role => role.Value),
        claims = ((ClaimsPrincipal)User).Claims
            .Where(c => c.Type.StartsWith("app-", StringComparison.OrdinalIgnoreCase))
            .Select(role => new { role.Type, role.Value })
        },
        new { id = "userDetails" })
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink", app_login_link = "" })</li>
    </ul>
    
    @Html.Json(new {
        isAuthenticated = false,
        userName = (string)null,
        userId = (string)null,
        roles = Enumerable.Empty<string>()
        },
        new { id = "userDetails" })
}
